let list = [
    {
        id: 1,
        title: '大方点撒',
        img: 'https://img.alicdn.com/imgextra/i4/1141970038/O1CN014bPHnh1C9RnLAOkr1_!!0-saturn_solar.jpg_468x468q75.jpg',
        price: 13.00,
        num: 0,
    },
    {
        id: 2,
        title: '鱼子家',
        img: 'https://img.alicdn.com/imgextra/i4/1141970038/O1CN014bPHnh1C9RnLAOkr1_!!0-saturn_solar.jpg_468x468q75.jpg',
        price: 1232.00,
        num: 0,
    },
    {
        id: 3,
        title: '裤子',
        img: 'https://img.alicdn.com/imgextra/i4/1141970038/O1CN014bPHnh1C9RnLAOkr1_!!0-saturn_solar.jpg_468x468q75.jpg',
        price: 1223.00,
        num: 0,
    },
    {
        id: 4,
        title: '上衣',
        img: 'https://img.alicdn.com/imgextra/i4/1141970038/O1CN014bPHnh1C9RnLAOkr1_!!0-saturn_solar.jpg_468x468q75.jpg',
        price: 123.00,
        num: 0,
    },
    {
        id: 5,
        title: '帽子',
        img: 'https://img.alicdn.com/imgextra/i4/1141970038/O1CN014bPHnh1C9RnLAOkr1_!!0-saturn_solar.jpg_468x468q75.jpg',
        price: 113.00,
        num: 0,
    },
    {
        id: 6,
        title: '手表',
        img: 'https://img.alicdn.com/imgextra/i4/1141970038/O1CN014bPHnh1C9RnLAOkr1_!!0-saturn_solar.jpg_468x468q75.jpg',
        price: 128.00,
        num: 0,
    },
]
let goods = []
const shopReduce = function (preState = { list: list, goods: goods }, action) {
    console.log(action, 'action');
    let { type, data } = action
    switch (type) {
        case 'addshoped':

            let arr = [] // 创建一个空数组
            let getData = preState.goods
            if (getData) {  // 如果有，那么进行转换使用
                arr = JSON.parse(JSON.stringify(preState.goods))
            }

            // 在数据中进行查找，路由传值过来的id是否存在
            let index = arr.findIndex((item, index) => {
                return item.id === data.id
            })
            // 如果有进行数量加一，如果没有数组进行push添加操作
            if (index !== -1) {
                arr[index].num++
                preState.goods = arr

            } else {
                data.num++
                arr.push(data)
                preState.goods = arr


            }

            preState = JSON.parse(JSON.stringify(preState))

            return preState;

        case "addnum":
            let newPreState = JSON.parse(JSON.stringify(preState))
            
            let a = newPreState.goods.findIndex(item => { return item.id === data })
            newPreState.goods[a].num++
            return newPreState

        case "jiannum":
            let b = preState.goods.findIndex(item => { return item.id === data })
            if (preState.goods[b].num > 1) {
                preState.goods[b].num--
            }
            return preState
        case "delitem":
            let c =  preState.goods.findIndex(item => { return item.id === data })
            preState.goods.splice(c, 1)
            preState = JSON.parse(JSON.stringify(preState))

            console.log(preState.goods);

            return preState
        case "delall":
            let new_arr = preState.goods.filter((x) => !data.some((item) => x.id === item));
            preState.goods = new_arr
            preState = JSON.parse(JSON.stringify(preState))

            console.log(preState);
            return preState;
            
        
        default:
            return preState;
    }


}

export default shopReduce